<template>
  <!-- 弹出框 -->
  <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'编辑通知信息':'新增通知'">
    <el-form ref="noticeForm" autocomplete="on" :rules="noticeRules" :model="notice" label-width="80px"
      label-position="left">
      <el-form-item label="主题" prop="subject">
        <el-input v-model="notice.subject" placeholder="输入主题" ref="subject" name="subject" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input type="textarea" :rows="5" placeholder="请输入内容" class="article-textarea" v-model="notice.content"
          maxlength="500" ref="content" name="content">
        </el-input>
      </el-form-item>
    </el-form>
    <div style="text-align:right;">
      <el-button type="primary" @click="confirmNotice">提交</el-button>
      <el-button type="danger" @click="closeNotice">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    MessageBox,
    Message
  } from 'element-ui'
  import {
    addNotice,
    updateNotice,
  } from '@/api/notice'
  import store from '@/store'
  export default {
    props: {
      "dialogType": {
        default: 'new',
        type: String
      },
      "dialogVisible": {
        default: false,
        type: Boolean
      },
      "notice": {
        type: Object
      }
    },
    data() {
      return {
        noticeRules: {
          subject: [{
            required: true,
            trigger: 'blur'
          }],
          content: [{
            required: true,
            trigger: 'blur'
          }]
        },
      }
    },
    methods: {
      // 提交数据
      confirmNotice() {
        if (this.dialogType === 'new') {
          // 提交新增用户表单
          const notice = this.notice
          // 校验表单数据
          this.$refs.noticeForm.validate(async valid => {
            if (valid) {
              const res = await addNotice(notice,store.getters.token)
              if (res.code === 1) {
                this.$emit("dialogDataAdd", res.data)
                this.closeNotice()
                Message({
                  message: res.msg,
                  type: 'success',
                  duration: 5 * 1000
                })
              }
            } else {
              console.log('error submit!!')
              return false
            }
          })
        } else {
          // 提交编辑
          const notice = this.notice
          // 校验表单数据
          this.$refs.noticeForm.validate(async valid => {
            if (valid) {
              const res = await updateNotice(notice)
              if (res.code === 1) {
                this.$emit("dialogDataChange",notice.id,notice)
                this.closeNotice()
                Message({
                  message: res.msg,
                  type: 'success',
                  duration: 5 * 1000
                })
              }
            } else {
              console.log('error submit!!')
              return false
            }
          })
        }
      },
      // 关闭表单
      closeNotice() {
        this.$emit("update:dialogVisible", false)
      }
    },
    created() {
    }
  }
</script>

<style>
</style>
